import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

import 'action.dart';
import 'state.dart';

var formatter = DateFormat('yyyy-MM-dd HH:mm:ss');

Widget buildView(
    DeviceLogState state, Dispatch dispatch, ViewService viewService) {
  return Scaffold(
    appBar: AppBar(
      title: Text('${state.deviceName}的日志'),
    ),
    body: ListView(
      children: _createLogList(state, dispatch, viewService),
    ),
    floatingActionButton: FloatingActionButton(
      child: Icon(Icons.refresh),
      onPressed: () {
        dispatch(DeviceLogActionCreator.getDeviceLog(state.deviceNo, 1));
      },
    ),
  );
}

List<Widget> _createLogList(
    DeviceLogState state, Dispatch dispatch, ViewService viewService) {
  var list = List<Widget>();
  state.logList.forEach((log) {
    var item = Padding(
      padding: EdgeInsets.only(left: 8, top: 8, right: 8),
      child: ListTile(
        title: Text(
          formatter.format(DateTime.fromMillisecondsSinceEpoch(log.createTime)),
          style: TextStyle(fontSize: 16),
        ),
        subtitle: Text(
          log.logUrl,
          style: TextStyle(
              fontSize: 12,
              fontStyle: FontStyle.italic,
              color: Colors.grey[300]),
        ),
        onTap: () {
          dispatch(DeviceLogActionCreator.scanLogContent(log));
        },
      ),
    );
    list.add(item);
  });
  return list;
}
